package com.kdtech.analyse.blog;
import com.kdtech.crawler.CrawlHTML;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;


import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.analyse.AnalyseNews;

/**
 * མཚོ་སྔོན་པོ་བོད་ཡིག་ཟིན་བྲིས། བོད་ཡིག ཟིན་བྲིས མཚོ
 * @author KK
 *
 */
public class AmdotibetBlogAnalyse implements AnalyseNews{

	
	public boolean isDetailPage(String url) {
		String[] regex = {
				"http://blog.amdotibet.cn/redirect.aspx\\?go=log&id=[0-9]*&userid=[0-9]*",
				};
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt = urlMeta.getHtml();
		String newUrl = StringUtils.substringBetween(htmltxt, "='", "'");
		if(StringUtils.isNotBlank(newUrl)){
//			UrlMeta meta = CrawlHTML.responseToURL(newUrl);
			UrlMeta meta = null;
			htmltxt = meta.getHtml();
		}
		String url = urlMeta.getUrl();
		String title = null;
		String content = null;
		Long date = null;
		Integer commnetNum = null;
		Integer clickNum = null;
		Integer deliverNum = null;
		Document doc = Jsoup.parse(htmltxt);
		String author=null;

		title = doc.select("div#userblog_topic").text();
		date = DateUtils.matchDate(doc.select("div.time").text()) ;
		if(date==null){
			date = DateUtils.matchDate(doc.select("td.time").text()) ;
		}
		if(date==null){
			date = DateUtils.matchDate(doc.select("div[align=right]").text()) ;
		}
		if(date==null){
			date = DateUtils.matchDate(doc.select("span#usercontents").text()) ;
		}
		content = HtmlCleaner.getContentHtml(url,  doc.select("div.logtext"));
		if(StringUtils.isBlank(content)){
			content = HtmlCleaner.getContentHtml(url,  doc.select("td.logt"));
		}
		if(StringUtils.isBlank(content)){
			content = HtmlCleaner.getContentHtml(url,  doc.select("span#usercontents"));
		}
		String commTxt = doc.select("div#com").text();
		if(StringUtils.isBlank(commTxt)){
			commTxt = doc.select("td#com").text();
		}
		clickNum =NumberUtils.matchNumber(StringUtils.substringBefore(commTxt, "| "));
		commnetNum =NumberUtils.matchNumber(StringUtils.substringAfter(commTxt, "| "));

			NewsMeta news = new NewsMeta();
			news.setUrl(url);
			news.setType(7);
			news.setAuthor(author);
			news.setTitle(StringUtils.trimSpace(title));
			news.setCommentNum(commnetNum);
			news.setClickNum(clickNum);
			//news.setDeliverNum(deliverNum);
			news.setContent(StringUtils.trimSpace(content));
			news.setDate(date);
			news.setUpdateUrl(newUrl);
			return news;
	}

/*	
	public NewsMeta Update(NewsMeta meta) {
		if (meta!=null &&StringUtils.isNotBlank(meta.getUpdateUrl())){
			UrlMeta urlMeta = CrawlHTML.responseToURL(meta.getUpdateUrl());
			if (urlMeta!=null && urlMeta.getHtml()!=null){
				return parserHtml(urlMeta);
			}
		}
		return null;
	}

	*/

	
}
